import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { getMailList, getSearchList ,getFileList} from "@/services"
import { ImailList, IsearchList,IfileList } from "../utils/interface"
export interface IndexModelState {
  name: string,
  mailList: ImailList[],
  searchList: IsearchList[],
  fileList:IfileList[]
}

export interface IndexModelType {
  namespace: 'mail';
  state: IndexModelState;
  effects: {
    query: Effect;
    search: Effect;
    file:Effect
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions?: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'mail',

  state: {
    name: '',
    mailList: [],
    searchList: [],
    fileList:[],
  },

  effects: {
    *query({ payload }, { call, put }) {
      let { from, to, subject } = payload
      let res = yield call(getMailList, { from, to, subject })
      console.log(res.data[0]);
      yield put({
        type: "save",
        payload: {
          mailList: res.data[0]
        }
      })
    },
    *search({ payload }, { call, put }) {
      let { type, keyword, count } = payload
      let res = yield call(getSearchList, { type, keyword, count })
      console.log(res.data[0]);
      yield put({
        type: "save",
        payload: {
          searchList: res.data[0]
        }
      })
    },
    *file({ payload }, { call, put }) {
      // let { type, keyword, count } = payload
      let res = yield call(getFileList)
      console.log(res.data[0]);
      yield put({
        type: "save",
        payload: {
          fileList: res.data[0]
        }
      })
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  //   subscriptions: {
  //     setup({ dispatch, history }) {
  //       return history.listen(({ pathname }) => {
  //         if (pathname === '/') {
  //           dispatch({
  //             type: 'query',
  //           });
  //         }
  //       });
  //     },
  //   },
};

export default IndexModel;